package com.wider.servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wider.json.*;
/**
 * Servlet implementation class NxTest
 */
public class NxTest extends HttpServlet {
	private class infoPktCls{
		public boolean isfolder;
		public boolean exists;
		public long lastmod;
		public long filesize;
		void setValue(boolean inisfolder,boolean inexists,long inlastmod,long infilesize){
			isfolder=inisfolder;
			exists=inexists;
			lastmod=inlastmod;
			filesize=infilesize;
		}
	}
	private class listPktCls{
		private String[] listData;
		void setValue(String[] inlistData){
			listData=inlistData;
		}
	}
	private static final long serialVersionUID = 1L;
    public NxTest() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject jsonpkt = null;
		infoPktCls info = new infoPktCls();
		infoPktCls pak = null;
		
		String command=request.getParameter("command");
		String path=request.getParameter("path");
		
		
		if(command.equals("getinfo")){
			File fpath=new File(path);


			jsonpkt=new JSONObject();
			try {
				jsonpkt.put("statuscode", 1);
				jsonpkt.put("isfolder", fpath.isDirectory());
				jsonpkt.put("exists", fpath.exists());
				jsonpkt.put("lastmod", fpath.lastModified());
				jsonpkt.put("filesize", fpath.length());
				PrintWriter pnd;
				pnd=response.getWriter();
				pnd.write(jsonpkt.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(jsonpkt + " and " + jsonpkt.toString());

		}
		else if (command.equals("filelist")){
			File fpath=new File(path);
			jsonpkt=new JSONObject();
			
			try {
				jsonpkt.put("statuscode", 2);
				jsonpkt.put("list", fpath.list());
				
				PrintWriter pnd;
				pnd=response.getWriter();
				pnd.write(jsonpkt.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}

		

		
		//pnd.write("123!!!!!");
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

}
